package com.sec.android.app.myfiles.decorator.tablet;

import android.app.ActionBar;
import android.content.Context;
import android.view.Menu;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.decorator.MenuImpDecorator;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment.ActionModeType;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.IMenuImp;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.util.UiUtils;

public class TabletMenuImpDecorator extends MenuImpDecorator {

    private AbsMyFilesFragment mFragment;
    private NavigationInfo mCurNavigationInfo;

    public TabletMenuImpDecorator(Context context, IMenuImp menuImp) {
        super(context, menuImp);
    }

    @Override
    public void setActionBarNormal(ActionBar actionbar) {
        actionbar.setCustomView(null);

        actionbar.setDisplayHomeAsUpEnabled(false);
        actionbar.setDisplayShowTitleEnabled(true);
        actionbar.setHomeButtonEnabled(false);
        if (FileRecord.StorageType.OptimizeStorage.equals(mCurNavigationInfo.getStorageType())) {
            actionbar.setTitle(R.string.menu_get_more_space);
            actionbar.setDisplayHomeAsUpEnabled(true);
        } else {
            actionbar.setTitle(getActionBarTitle());
        }
    }

    @Override
    public void setInfo(AbsMyFilesFragment fragment, NavigationInfo navigationInfo) {
        super.setInfo(fragment, navigationInfo);
        mFragment = fragment;
        mCurNavigationInfo = navigationInfo;
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu, ActionModeType actionMode) {
        super.onPrepareOptionsMenu(menu, actionMode);
        if (mFragment != null && !mFragment.isSelectActionMode()) {
            boolean isSearch = false;
            boolean isGetMoreSpace = false;
            if (mCurNavigationInfo != null) {
                isSearch = mCurNavigationInfo.getStorageType() == FileRecord.StorageType.Search;
                isGetMoreSpace = mCurNavigationInfo.getStorageType() == FileRecord.StorageType.OptimizeStorage;
            }
            setMenuItemVisibility(menu, R.id.menu_optimize_storage, !isSearch && UiUtils.supportOptimizeStorage(mContext));
            setMenuItemVisibility(menu, R.id.menu_search, true);
            setMenuItemVisibility(menu, R.id.menu_toggle_list_type, !isGetMoreSpace);
            setMenuItemEnabled(menu, R.id.menu_toggle_list_type, !isSearch && (mFragment.getListItemCount() > 0 || mFragment.getLoadingStatus()));
        }
    }
}
